package com.example.shop.modules.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shop.dto.SaleParam;
import com.example.shop.modules.manage.service.IGoodsAdminService;
import com.example.shop.modules.manage.service.IOrderAdminService;
import com.example.shop.persistence.entity.Goods;
import com.example.shop.persistence.entity.Order;
import com.example.shop.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.example.shop.utils.JSONResult.renderSuccess;

/**
 * <p>
 * 后台统计 前端控制器
 * </p>
 *
 * @author jason
 * @since 2021.1.20
 */
@Api(tags = "后台接口首页统计")
@RestController
@RequestMapping("/manage/statistics")
public class StatisticsController {

    @Autowired
    private IOrderAdminService orderService;

    @Autowired
    private IGoodsAdminService goodsService;

    @ApiOperation(value = "销售情况统计")
    @GetMapping("/sale")
    public JSONResult getSale(){
        SaleParam saleParam = new SaleParam();
        // 查询待发货订单 数
        QueryWrapper<Order> ew = new QueryWrapper<>();
        ew.eq("state", 1100);
        saleParam.setDeliveredNumber(orderService.count(ew));

        // 查询当日订单数
        QueryWrapper<Order> wrapper2 = new QueryWrapper<>();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        // 按付款时间查询  查询大于等于今天的时间
        wrapper2.ge("payTime",format);
        List<Order> list = orderService.list(wrapper2);
        Integer con = 0;
        BigDecimal total = new BigDecimal(0);
        for(Order order: list){
            con++;
            total = total.add(order.getTotalMoney());
        }
        saleParam.setOrderNumber(con);
        saleParam.setTotalMoney(total);
        // 查询库存为0的商品数
        QueryWrapper<Goods> eg = new QueryWrapper<>();
        eg.eq("stock", 0);
        saleParam.setStockNumber(goodsService.count(eg));

        return renderSuccess(saleParam);
    }

}
